package com.gobuy.goods.controller;


import com.github.pagehelper.Page;
import com.gobuy.common.pojo.MessageEnum;
import com.gobuy.common.pojo.PageResult;
import com.gobuy.common.pojo.Result;
import com.gobuy.common.pojo.StatusCode;
import com.gobuy.common.utils.ResultUtil;
import com.gobuy.goods.pojo.Brand;
import com.gobuy.goods.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Gaozexi on 2020/6/2 21:49
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@RequestMapping("/brand")
@RestController
//开启跨域支持
@CrossOrigin
public class BrandController {

    @Autowired
    private BrandService brandService;
    /**
     * 查询品牌列表
     */
    @GetMapping
    public Result<List<Brand>> findList(){
        List<Brand> brandList = brandService.findList();
        return ResultUtil.success(MessageEnum.OK_SELECT_ALL,brandList);
        //return new Result<List<Brand>>(true, StatusCode.OK,"查询成功",brandList);
    }

    @GetMapping("/{id}")
    public Result<Brand> findById(@PathVariable("id") Integer id){
        Brand brand = brandService.findById(id);
        return new Result<Brand>(true, StatusCode.OK,"查询成功",brand);
    }

    @PostMapping
    public Result add(@RequestBody Brand brand){
        brandService.add(brand);
        return new Result(true, StatusCode.OK,"添加成功");
    }

    @PutMapping("/{id}")
    public Result update(@PathVariable("id")Integer id,@RequestBody Brand brand){
        brand.setId(id);
        brandService.update(brand);
        return new Result(true, StatusCode.OK,"修改成功");
    }

    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id")Integer id){
        brandService.delById(id);
        return new Result(true, StatusCode.OK,"删除成功");
    }


    @GetMapping("/search")
    public Result<List<Brand>> search(@RequestParam Map searchMap){
        List<Brand> brandList = brandService.searchByCondition(searchMap);
        return new Result<>(true, StatusCode.OK,"查询成功",brandList);
    }

    @GetMapping("/search/{page}/{size}")
    public Result<List<Brand>> findPage(@PathVariable("page") int page,
                                        @PathVariable("size") int size){
        Page<Brand> pageInfo = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/searchPage/{page}/{size}")
    public Result<List<Brand>> findPage(@RequestParam Map searchMap,@PathVariable int page,@PathVariable int size){

        Page<Brand> pageInfo = brandService.findPage(searchMap,page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result<>(true, StatusCode.OK,"查询成功",pageResult);
    }

    @GetMapping("/error")
    public Result<List<Brand>> findPage(){
        //测试统一异常处理类
        int i=1/0;
        return new Result<>(true, StatusCode.OK,"查询成功");
    }

    @GetMapping("/category/{categoryName}")
    public Result<List<Map>> findBrandListByCategoryName(@PathVariable("categoryName")String categoryName){
        List<Map> brandList = brandService.findBrandListByCategoryName(categoryName);
        return new Result<>(true, StatusCode.OK,"查询成功",brandList);
    }
}
